package com.example.materials_management.controller;

import com.example.materials_management.repository.ExportInfoRepository;
import com.example.materials_management.repository.ImportInfoRepository;
import com.example.materials_management.repository.MaterialsBasicInfoRepository;
import com.example.materials_management.repository.MaterialsRemainingInfoRepository;
import com.example.materials_management.util.SqlUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/sql")
public class SqlController {
    @Autowired
    SqlUtil sqlUtil;
    @Autowired
    ExportInfoRepository exportInfoRepository;
    @Autowired
    ImportInfoRepository importInfoRepository;
    @Autowired
    MaterialsBasicInfoRepository materialsBasicInfoRepository;
    @Autowired
    MaterialsRemainingInfoRepository materialsRemainingInfoRepository;

    @GetMapping("/init")
    public Map<String, Object> initSqlByDefaultData() {
        Map<String, Object> res = new HashMap<>();
        try {
            ClassPathResource sqlPath = new ClassPathResource("/static/materials_piyo_init.sql");
            sqlUtil.run(sqlPath);
            res.put("Status", "Success");

        } catch (Exception e) {
            e.printStackTrace();
            res.put("Status", "Error");
        }
        return res;
    }

    @GetMapping("/deleteAll")
    public Map<String, Object> deleteAll() {
        exportInfoRepository.deleteAll();
        importInfoRepository.deleteAll();
        materialsBasicInfoRepository.deleteAll();
        materialsRemainingInfoRepository.deleteAll();
        Map<String, Object> res = new HashMap<>();
        res.put("Status", "Success");
        return res;
    }
}
